package com.clouddisk.file.controller;

import com.clouddisk.common.model.PageResult;
import com.clouddisk.common.model.Result;
import com.clouddisk.file.dto.FileDTO;
import com.clouddisk.file.dto.FileUploadDTO;
import com.clouddisk.file.dto.FolderCreateDTO;
import com.clouddisk.file.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Tag(name = "文件管理")
@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @Operation(summary = "上传文件")
    @PostMapping("/upload")
    public Result<FileDTO> uploadFile(
            @Parameter(description = "用户ID") @RequestHeader("X-User-ID") Long userId,
            @Valid FileUploadDTO uploadDTO) {
        return Result.success(fileService.uploadFile(userId, uploadDTO));
    }

    @Operation(summary = "创建文件夹")
    @PostMapping("/folder")
    public Result<FileDTO> createFolder(
            @Parameter(description = "用户ID") @RequestHeader("X-User-ID") Long userId,
            @Valid @RequestBody FolderCreateDTO createDTO) {
        return Result.success(fileService.createFolder(userId, createDTO));
    }

    @Operation(summary = "获取文件列表")
    @GetMapping("/list")
    public Result<PageResult<FileDTO>> getFileList(
            @Parameter(description = "用户ID") @RequestHeader("X-User-ID") Long userId,
            @Parameter(description = "父文件夹ID") @RequestParam Long parentId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(fileService.getFileList(userId, parentId, pageNum, pageSize));
    }

    @Operation(summary = "删除文件")
    @DeleteMapping("/{fileId}")
    public Result<Void> deleteFile(
            @Parameter(description = "用户ID") @RequestHeader("X-User-ID") Long userId,
            @Parameter(description = "文件ID") @PathVariable Long fileId) {
        fileService.deleteFile(userId, fileId);
        return Result.success();
    }

    @Operation(summary = "下载文件")
    @GetMapping("/download/{fileId}")
    public ResponseEntity<byte[]> downloadFile(
            @Parameter(description = "用户ID") @RequestHeader("X-User-ID") Long userId,
            @Parameter(description = "文件ID") @PathVariable Long fileId) {
        byte[] fileBytes = fileService.downloadFile(userId, fileId);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment")
                .body(fileBytes);
    }

    @Operation(summary = "获取用户已使用的存储空间")
    @GetMapping("/storage/used")
    public Result<Long> getUserStorageUsed(
            @Parameter(description = "用户ID") @RequestHeader("X-User-ID") Long userId) {
        return Result.success(fileService.getUserStorageUsed(userId));
    }
} 